#include<iostream>
#include"lexical/lexical.h"
#include"syntax/syntax.h"

#define LEXICAL 1
#define SYNTAX 1

using namespace std;




int main(int argc, char *argv[])
{
    Symbol_Table(my_symbol_table);

#if LEXICAL // 词法分析阶段
    if(argc == 1)
    {
        preprocess((char*)"./inter_txt/test.txt", (char*)"./inter_txt/pre_processed.txt");
    }
    else{
        preprocess(argv[1], (char*)"./inter_txt/pre_processed.txt");
    }
    lex_analyze((char*)"./inter_txt/pre_processed.txt", (char*)"./inter_txt/lex_analyzed.txt", &my_symbol_table);
    my_symbol_table.print_symbol(); // 将符号表打印至文件中
#endif

#if SYNTAX // 语法分析阶段
    
    int item_num = get_item((char *)"./syntax/grammar", start_symbol, expand_start_symbol);

    item_info my_item[item_num]; // 所有的项目，具体信息见定义
    vector<vector<int>> family_member; // 项目的状态集合，定义为family_member[i][j]为第i个状态的第j个项目
    vector<item_family_edge> my_edge; // 转换边，具体信息见定义
    int family_num = get_item_family((char *)"./inter_txt/item.txt", item_num, my_item, family_member, my_edge); // 通过此函数获取以上三组结构信息

    int state_table[family_num][TERMINAL_SYMBOL_NUM+NON_TERMINAL_SYMBOL_NUM+1];
    init_state_table(state_table, family_num);

    bool FIRST_set[NON_TERMINAL_SYMBOL_NUM][TERMINAL_SYMBOL_NUM+1];
    bool FOLLOW_set[NON_TERMINAL_SYMBOL_NUM][TERMINAL_SYMBOL_NUM+1];
    init_set(FIRST_set, FOLLOW_set);
    get_FOLLOW(FIRST_set, FOLLOW_set, my_item, item_num);
    /*
    状态表每行第0的位置留给#，1~TERMINAL_SYMBOL_NUM 为终结符，后面为非终结符
    状态表每一项：
    -3 INIT 在调试时用宏定义，不要直接用数字
    -2 ERROR
    -1 ACC
    0 ~ family_num-1 跳转状态索引
    family_num~family_num+item_num 项目索引，从family_num开始的原因是与状态编号区分开，当i>=family_num时，查找my_item[i-family_num]对应的产生式信息，进行归约
    */

    // 到这里才发现，family用state命名更合适一些.. 将错就错吧先

    int acc_state = get_state_table(state_table, family_num, my_item, item_num, family_member, my_edge, FOLLOW_set);

    // syn_analyze(); // 分析
    syntax_analyze((char *)"./inter_txt/lex_analyzed.txt", state_table, family_num, my_item, acc_state);
    
#endif

    return EXIT_SUCCESS;
}

